const { app, Menu, nativeTheme, shell } = require("electron");

const isMac = process.platform === "darwin";
const { openFileAsString } = require("../handlers");

/**
 * 主题改变之后通知
 * @param {*} mainWindow 
 */
function changeTheme(mainWindow) {
  mainWindow.send('theme-changed', nativeTheme.themeSource)
}

function buildAppMenu(mainWindow) {
  nativeTheme.themeSource = 'system'
  const { grammerMenuTemplate } = require("./grammerMenu.js");
  const grammerMenu = grammerMenuTemplate(mainWindow);

  const menuTemplate = [
    ...(isMac
      ? [
          {
            label: "Markdown Editor",
            submenu: [
              { role: "about", label: "关于" },
              { type: "separator" },
              { role: "services", label: "服务" },
              { type: "separator" },
              { role: "hide", label: "隐藏" },
              { role: "hideOthers", label: "隐藏其他" },
              { role: "unhide", label: "显示" },
              { type: "separator" },
              { role: "quit", label: "退出" },
            ],
          },
        ]
      : []),
    {
      role: "fileMenu",
      label: "文件",
      submenu: [
        {
          label: "新建文档",
          role: "add",
          id: "addFile",
          accelerator: isMac ? "Cmd + N" : "Ctrl + N",
          click: () => {
            mainWindow.webContents.send("add-new-file");
          },
        },
        {
          label: "打开文档",
          role: "open",
          id: "openFile",
          accelerator: isMac ? "Cmd + Shift + O" : "Ctrl + Shift + O",
          click: () => {
            const fileData = openFileAsString();
            if (fileData)
              mainWindow.webContents.send("open-file-string", fileData);
          },
        },
        { type: "separator" },
        {
          label: "关闭",
          role: "closeFile",
          id: "closeFile",
          accelerator: isMac ? "Cmd + W" : "Ctrl + W",
          click: () => {
            mainWindow.webContents.send("close-file");
          },
        },
        {
          label: "保存",
          role: "save",
          id: "saveFile",
          accelerator: isMac ? "Cmd + S" : "Ctrl + S",
          click: () => {
            console.log(mainWindow);
            mainWindow.webContents.send("save-file-menu");
          },
        },
      ],
    },
    // 编辑菜单
    {
      role: "editMenu",
      label: "编辑",
      submenu: [
        { role: "undo", label: "撤销" },
        { role: "redo", label: "重做" },
        { type: "separator" },
        { role: "cut", label: "剪切" },
        { role: "copy", label: "复制" },
        { role: "paste", label: "粘贴" },
        { role: "pasteAndMatchStyle", label: "粘贴并匹配格式" },
        { role: "delete", label: "删除" },
        { role: "selectAll", label: "全选" },
        { type: "separator" },
        { role: "showSubstitutions", label: "查找" },
      ],
    },
    // 语法
    { ...grammerMenu },
    {
      role: "viewMenu",
      label: "视图",
      submenu: [
        // 开发环境显示开发者菜单
        ...(app.isPackaged
          ? []
          : [
              { role: "reload" },
              { role: "forceReload" },
              { role: "toggleDevTools" },
              { type: "separator" },
            ]),
        { role: "resetZoom", label: "实际大小" },
        { role: "zoomIn", label: "放大" },
        { role: "zoomOut", label: "缩小" },
        { type: "separator" },
        { role: "togglefullscreen", label: "全屏" },
        { type: "separator" },
        // 模式。可选值：edit(纯编辑模式) editable(编辑与预览模式) preview(纯预览模式)。
        {
          role: "edit",
          label: "编辑模式",
          accelerator: isMac ? "Cmd + Alt + E" : "Ctrl + Alt + E",
          click: () => {
            mainWindow.webContents.send("view-menu", {
              type: "mode",
              value: "edit",
            });
          },
        },
        {
          role: "editable",
          label: "编辑与预览",
          accelerator: isMac ? "Cmd + Alt + B" : "Ctrl + Alt + B",
          click: () => {
            mainWindow.webContents.send("view-menu", {
              type: "mode",
              value: "editable",
            });
          },
        },
        {
          role: "preview",
          label: "预览模式",
          accelerator: isMac ? "Cmd + Alt + P" : "Ctrl + Alt + P",
          click: () => {
            mainWindow.webContents.send("view-menu", {
              type: "mode",
              value: "preview",
            });
          },
        },
        { type: "separator" },
        {
          role: "theme",
          label: "主题",
          submenu: [
            {
              label: '浅色',
              type: 'radio',
              checked: nativeTheme.themeSource === 'light',
              click: () => {
                nativeTheme.themeSource = 'light'
                changeTheme(mainWindow)
              },
            },
            {
              label: '深色',
              type: 'radio',
              checked: nativeTheme.themeSource === 'dark',
              click: () => {
                nativeTheme.themeSource = 'dark'
                changeTheme(mainWindow)
              },
            },
            {
              label: '跟随系统',
              type: 'radio',
              checked: nativeTheme.themeSource === 'system',
              click: () => {
                nativeTheme.themeSource = 'system'
                changeTheme(mainWindow)
              },
            }
          ]
        },
      ],
    },
    { role: "windowMenu", label: "窗口" },
    {
      role: "help",
      label: "帮助",
      submenu: [
        {
          label: "检查更新",
          click: async () => {
            await shell.openExternal("https://github.com/WDFM/markdown-editor..git");
          },
        },
        {
          label: "使用帮助",
          click: async () => {
            await shell.openExternal("http://ckang1229.gitee.io/vue-markdown-editor/zh/");
          },
        },
      ],
    },
  ];

  const menus = Menu.buildFromTemplate(menuTemplate);
  Menu.setApplicationMenu(menus);
};

function showContextMenu(mainWindow) {
  const { grammerSubMenu } = require("./grammerMenu.js");
  const grammerMenu = grammerSubMenu(mainWindow);

  const menu = Menu.buildFromTemplate(grammerMenu)
  menu.popup()
}

function buildDockMenu() {
  const dockMenu = Menu.buildFromTemplate([
    {
      label: "新建文档",
      click: () => {
        mainWindow.webContents.send("add-new-file");
      },
    },
    {
      label: "打开文档",
      click: () => {
        const fileData = openFileAsString();
        if (fileData)
          mainWindow.webContents.send("open-file-string", fileData);
      },
    }
  ])
  app.dock.setMenu(dockMenu)
}

module.exports = {
  buildAppMenu,
  showContextMenu,
  buildDockMenu
}
